<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Chapter 5.&nbsp; Functions</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-5.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-5-SECT-1.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-5"></a>
<h2 id="title-IDAGQ4LM" class="docChapterTitle">Chapter 5. Functions</h2>
<p class="docText">To write programs in PHP that contain more than just a couple of pages of code and are still organized enough to be useful, you need to understand <span class="docEmphasis">functions</span>.<a name="IDX-CHP-5-0169"></a> 
 Functions provide a way to eliminate repeating the same lines of code over and over in your programs. Functions work by assigning a name to a chunk of code, called a function name. Then you execute the code by calling that name.</p>
<p class="docText">There are hundreds of built-in functions in PHP. For example, <tt>print_r</tt> is a function that prints readable information about a variable in plain English rather than code.</p>
<p class="docText">If given a string, integer, or float, the value itself is printed with the <tt>print_r</tt> function. If given an array, values are shown as keys and elements. A similar format is used for objects. With the advent of PHP 5.0, <tt>print_r</tt> and <tt>var_export</tt> show protected and private properties of objects.</p>
<p class="docText">Functions run the gamut from <tt>aggregate_info</tt> to <tt>imap_ping</tt> through <tt>pdf_open_image</tt>. Since there are so many, we can only cover some basics in this chapter, but we'll give you enough information that you'll be using functions like a pro in no time at all. You can search <a class="docLink" target="_blank" href="http://www.php.net">http://www.php.net</a> for an exhaustive list of functions.</p>
<p class="docText">Specifically, we'll go over the following:</p>
<ul><li><p class="docList">How to create a function, give it a name, and execute that function</p></li><LI><p class="docList">How to send values to a function and use them in the function</p></li><LI><p class="docList">How to return values from a function and use them in your code</P></li><LI><p class="docList">How to verify a function exists before you try using it</p></li></ul>
<p class="docText">When to split out code into a function is a bit of a judgment call. Certainly, if you find yourself repeating several lines of code over and over, it makes sense to pull that code into its own function. That will make your code easier to read and also prevent you from having to make a lot of changes if you decide to do something different with that block of code, as it's then only in one spot, not numerous places where you'd have to search and replace to change it.</P>
<p class="docText">A function is a block of code that accepts values, processes them, and then performs an action. A function doesn't need to accept values, doesn't have to process anything, and doesn't have to perform an action, other than to return control at the end. Think of making cookies and baking them in oven as a function. You put the raw cookie dough into the oven, which makes the cookie dough the input. The oven bakes the cookie dough; this is the function. The result of the bake function is the edible, baked cookies. The bake function might even take other inputs, such as temperature and bake time. These various inputs are called <span class="docEmphasis">parameters</span>.<a name="IDX-CHP-5-0170"></a> 
</p>
<p class="docText">Parameters send information to a function, and then the function executes the code. Functions can use anywhere from zero parameters to a whole list of them. In this example, you'll use the <tt>echo</tt> function to display some text. <tt>echo</tt> displays text that you send to it as a parameter. Most functions require you to place their parameters inside of parentheses, but <tt>echo</tt> is an exception to this rule. Echoing of all variables is nearly foolproof!</p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-5-EX-1">Example 5-1</a> shows about as basic of a program as you can get.</P>
<a name="learnphpmysql-CHP-5-EX-1"></a><h5 id="title-IDA4S4LM" class="docExampleTitle">Example 5-1. The ubiquitous Hello world!</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
echo ("Hello world!");
?&gt;
</pre><br>

</td></TR></table></P>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-5-FIG-1">Figure 5-1</a> shows how the output of the script appears in a browser.</p>
<a name="learnphpmysql-CHP-5-FIG-1"></a><p><center>
<h5 class="docFigureTitle">Figure 5-1. How the echo output looks in the browser window</h5>
<img border="0" alt="" width="454" height="218" SRC="images/learnphpmysql_0501.jpg">
</center></P><br>
<p class="docText">The <tt>echo</tt> function simply passes on the "Hello world!" string to the browser once you load the PHP file.</P>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><TR><TD><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><TR><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><td valign="top">
<p class="docText"><tt>echo</tt> is actually a PHP language construct. Practically, this translates to its ability to work without enclosing its parameters in parentheses. It's worthy to note that true functions always require parentheses.</p>
</td></tr></table></td></tr></table></p><br>
<p class="docText">You can use one of PHP's many built-in functions or define your own. We'll talk more about defining other functions later in this chapter.</p>

<a href="11011536.html"><img src="images/pixel.jpg" alt="" width="1" height="1" border="0"></a></TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-5.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-5-SECT-1.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
</body></html>
